<template>
    <div class="Teachers">
        <el-row>
            <el-col :span="24">
                <div class="grid-content bg-purple-dark">
                    <el-form :inline="true" :model="queryParams" class="menu-form-inline">
                        <el-form-item label="班主任">
                            <el-input v-model="queryParams.sysUser.nickName" placeholder="请输入班主任姓名"></el-input>
                        </el-form-item>

                        <el-form-item label="班主任手机号">
                            <el-input v-model="queryParams.sysUser.phonenumber" placeholder="请输入班主任手机号"></el-input>
                        </el-form-item>

                        <el-form-item label="年级">
                            <el-select v-model="queryParams.shClass.grade" placeholder="年级"
                                @change="getClassByGrade(queryParams.shClass.grade)">
                                <el-option v-for="item in gradeOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="班级">
                            <el-select v-model="queryParams.shClass.className" placeholder="班级">
                                <el-option v-for="item in classOptions" :key="item.className"
                                    :label="item.className + '班'" :value="item.className">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search"
                                @click="findShTeachersPagination()">搜索</el-button>
                            <el-button icon="el-icon-refresh" @click="resetForm()">重置</el-button>

                            <el-button type="primary" @click="(dialogVisible = true), (title = '新增班主任'), clearDialog()"
                                icon="el-icon-plus" plain style="margin-left: 100px;">新增班主任</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <div class="grid-content bg-purple-dark">
                    <el-table :data="tableData" border height="350">
                        <el-table-column prop="sysUser.nickName" label="班主任姓名" align="center">
                        </el-table-column>
                        <el-table-column prop="sysUser.phonenumber" label="班主任手机号" align="center">
                        </el-table-column>
                        <el-table-column prop="shClass" label="负责班级" align="center">
                            <template slot-scope="scope">
                                <span v-if="scope.row.shClass.grade == 1">一年级</span>
                                <span v-if="scope.row.shClass.grade == 2">二年级</span>
                                <span v-if="scope.row.shClass.grade == 3">三年级</span>
                                <span v-if="scope.row.shClass.grade == 4">四年级</span>
                                <span v-if="scope.row.shClass.grade == 5">五年级</span>
                                <span v-if="scope.row.shClass.grade == 6">六年级</span>
                                <span>( {{ scope.row.shClass.className }} )班</span>
                            </template>
                        </el-table-column>

                        <el-table-column align="center" label="操作" width="500rpx">
                            <template slot-scope="scope">
                                <el-button @click="toUpdateShTeachers(scope.row)" type="text" size="small">编辑</el-button>
                                <el-button @click="delTeachers(scope.row)" type="text" size="small">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-col>
        </el-row>
        <el-row class="footer">
            <el-col :span="24">
                <div class="grid-content bg-purple-dark">
                    <div class="block">
                        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                            :current-page="pageNum" :page-sizes="[5, 10, 20, 30, 50]" :page-size="pageSize"
                            layout="total, sizes, prev, pager, next, jumper" :total="total">
                        </el-pagination>
                    </div>
                </div>
            </el-col>
        </el-row>

        <!-- 新增弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="500px">
            <el-form :model="shTeachers" :rules="rules" ref="shClass" label-width="100px" class="demo-ruleForm">
                <el-form-item label="班主任">
                    <el-select v-model="shTeachers.userId" placeholder="请选择">
                        <el-option v-for="item in teachersOptions" :key="item.userId" :label="item.nickName"
                            :value="item.userId">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="年级">
                    <el-select v-model="shTeachers.grade" placeholder="年级" @change="getClassByGrade2(shTeachers.grade)">
                        <el-option v-for="item in gradeOptions" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="班号">
                    <el-select v-model="shTeachers.classId" placeholder="班级">
                        <el-option v-for="item in classAddOptions" :key="item.id" :label="item.className + '班'"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="ifDialog()">确 定</el-button>
                <el-button @click="(dialogVisible = false), clearDialog()">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from "axios";
import { getToken } from '@/utils/auth';

export default {
    name: "Teachers",
    data() {
        return {
            tableData: [],
            queryParams: {
                shClass: {
                    grade: "",
                    className: ""
                },
                sysUser: {
                    nickName: "",
                    phonenumber: ""
                }
            },
            total: 40,
            pageSize: 5,
            pageNum: 1,
            title: "",
            dialogVisible: false,
            shTeachers: {
                id: "",
                userId: "",
                classId: "",
                createTime: "",
                createBy: "",
                updateTime: "",
                updateBy: "",
                grade: ""
            },
            gradeOptions: [
                {
                    value: 1,
                    label: "一年级",
                },
                {
                    value: 2,
                    label: "二年级",
                },
                {
                    value: 3,
                    label: "三年级",
                },
                {
                    value: 4,
                    label: "四年级",
                },
                {
                    value: 5,
                    label: "五年级",
                },
                {
                    value: 6,
                    label: "六年级",
                },
            ],
            categoryIds: [],
            rules: {
                className: [
                    { required: true, message: "请输入班号", trigger: "blur" },
                ],
            },
            dialogVisible: false,
            disabled: false,
            classOptions: [],
            teachersOptions: [],
            classAddOptions: [],
        };
    },
    created() {
        this.findShTeachersPagination();
        this.getTeachersList();
    },
    methods: {
        // 班级列表分页查询
        findShTeachersPagination() {
            axios
                .post(
                    "/api/manager/teachers/findShTeachersPagination?pageNum=" +
                    this.pageNum +
                    "&pageSize=" +
                    this.pageSize,
                    this.queryParams
                )
                .then((res) => {
                    console.log(res)
                    // console.log(res)
                    this.tableData = res.data.data.items;
                    this.total = res.data.data.totalElements;
                });
        },
        // 每页条数改变时调用班主任列表分页查询
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.pageSize = val;
            this.findShClassPagination();
        },
        // 页码改变时调用班主任列表分页查询
        handleCurrentChange(val) {
            console.log(val);
            this.pageNum = val;
            this.findShTeachersPagination();
        },
        // dialog 表单提交后清空表单
        clearDialog() {
            for (let key in this.shTeachers) {
                if (this.shTeachers.hasOwnProperty(key)) {
                    // 确保属性是对象自身的，而不是继承的
                    this.shTeachers[key] = ""; // 或者设置为 null
                }
            }
        },
        // 条件查询信息清空 重置
        resetForm() {
            this.queryParams.sysUser.nickName = "";
            this.queryParams.sysUser.phonenumber = "";
            this.queryParams.shClass.className = "";
            this.queryParams.shClass.grade = "";
            this.findShTeachersPagination();
        },
        // 班主任删除
        delTeachers(row) {
            axios.get("/api/manager/teachers/deleteShTeachersById?id=" + row.id).then((res) => {
                console.log(res.data);
                if (res.data.code == 200) {
                    this.$message({
                        message: "恭喜你," + res.data.message,
                        type: "success",
                    });
                    this.findShTeachersPagination();
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 班主任新增
        addTeachers() {
            this.shTeachers.createBy = localStorage.getItem('nickName');
            this.shTeachers.updateBy = localStorage.getItem('nickName');
            axios.post("/api/manager/teachers/addShTeachers", this.shTeachers).then((res) => {
                if (res.data.code == 200) {
                    this.$message({
                        message: "恭喜你，" + res.data.message,
                        type: "success",
                    });
                    this.dialogVisible = false;
                    this.clearDialog();
                    this.findShTeachersPagination();
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 班主任回显
        toUpdateShTeachers(row) {
            this.title = "修改班主任";
            this.getClassList()
            axios.post("/api/manager/teachers/getShTeachersById", row).then((res) => {
                if (res.data.code == 200) {
                    console.log(res);
                    this.shTeachers = res.data.data;
                    this.shTeachers.userId = res.data.data.sysUser.userId;
                    this.shTeachers.grade = res.data.data.shClass.grade;
                    this.shTeachers.classId = res.data.data.shClass.id;
                    console.log(res.data.data)
                    this.dialogVisible = true;
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 班主任修改
        updateTeachers() {
            this.shTeachers.updateBy = localStorage.getItem('nickName');
            axios.post("/api/manager/teachers/updateShTeachersById", this.shTeachers).then((res) => {
                if (res.data.code == 200) {
                    this.$message({
                        message: "恭喜你，" + res.data.message,
                        type: "success",
                    });
                    this.dialogVisible = false;
                    this.clearDialog();
                    this.findShTeachersPagination();
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 判断 title 提交 新增/修改 方法
        ifDialog() {
            if (this.title == "新增班主任") {
                this.addTeachers();
            }
            if (this.title == "修改班主任") {
                this.updateTeachers();
            }
        },
        getClassByGrade(grade) {
            console.log(grade)
            axios.post("/api/manager/class/getShClassList?grade=" + grade).then((res) => {
                this.classOptions = res.data.data;
                console.log(res.data)
            });
        },
        getClassByGrade2(grade) {
            console.log(grade)
            axios.post("/api/manager/class/getShClassList?grade=" + grade).then((res) => {
                this.classAddOptions = res.data.data;
                console.log(res.data)
            });
        },
        getTeachersList() {
            axios.post("/api/manager/sysUser/getTeachersList").then((res) => {
                this.teachersOptions = res.data.data;
                console.log(res.data)
            });
        },
        getClassList() {
            axios.post("/api/manager/class/getShClassListAll").then((res) => {
                this.classAddOptions = res.data.data;
                console.log(res.data)
            });
        },

    },

};
</script>

<style scoped>
.el-row {
    margin-top: 10px;
}

.footer {
    margin-top: 20px;
    text-align: right;
}
</style>